package com.gexiaobao.pigeon.ui.view;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;

import androidx.core.content.ContextCompat;

import com.gexiaobao.pigeon.R;

/**
 * @Author : hxw
 * @Date : 2024/5/28 16:55
 * @Describe :
 */
public class PieProgressBar extends View {

    private Paint paint;
    private int progress = 0;
    private int max = 100;
    private int startAngle = 0;

    public PieProgressBar(Context context) {
        super(context);
        init();
    }

    public PieProgressBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public PieProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setStyle(Paint.Style.FILL);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int width = getWidth();
        int height = getHeight();
        int radius = Math.min(width, height) / 2;

        paint.setColor(Color.LTGRAY); // 背景颜色
        canvas.drawCircle(width / 2, height / 2, radius, paint);

        paint.setColor(ContextCompat.getColor(getContext(), R.color.color_003366)); // 进度条颜色
        canvas.drawArc(new RectF(width / 2 - radius, height / 2 - radius, width / 2 + radius, height / 2 + radius), startAngle, ((float) progress / max) * 360, true, paint);
    }

    public void setProgress(int progress) {
        this.progress = progress;
        invalidate();
    }

    public void setMax(int max) {
        this.max = max;
    }
}
